package com.tjsinfo.tjsoa.addresslist.bo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending;

public class AddressListAndFilePendingBo implements IAddressListAndFilePendingBo {
	private IJDBCDao jdbcDao;
	private IQbcDao qbcDao;

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public void setJdbcDao(IJDBCDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo#save(com.tjsinfo.tjsoa.addresslist.vo.AddressListAndFilePending)
	 */
	public void save(AddressListAndFilePending pending) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String sql = "insert into dbo.t_addresslistAndFile_pending values('"+pending.getId()+"',"+pending.getSendUserId()+",'"+pending.getSendUserName()+"',"+pending.getTjsUserId()+",'"+pending.getTitle()+"','"+pending.getType()+"','"+pending.getUrl()+"','"+sdf.format(pending.getCreateTime())+"')";
		jdbcDao.executeUpdate(sql);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo#findByUserIdAndTyle(java.lang.Integer, java.lang.String)
	 */
	public List findByUserIdAndTyle(Integer userid,String type) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListAndFilePending.class);
		criteria.add(Restrictions.and(Restrictions.eq("tjsUserId", userid), Restrictions.eq("type", type)));
		criteria.addOrder(Order.desc("createTime"));
		
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo#findByUserIdAndTyle(java.lang.Integer, java.lang.String)
	 */
	public List findBySendUserIdAndTitle(Integer senduserid,String title) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListAndFilePending.class);
		criteria.add(Restrictions.and(Restrictions.eq("sendUserId", senduserid), Restrictions.eq("title", title)));
		criteria.addOrder(Order.desc("createTime"));
		
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo#deleteByUserIdAndTyle(java.lang.Integer, java.lang.String)
	 */
	public void deleteByUserIdAndTyle(Integer userid,String type) {
		String sql = "delete from dbo.t_addresslistAndFile_pending where tjsuser_id="+userid+" and type='"+type+"' ";
		jdbcDao.executeUpdate(sql);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.bo.IAddressListAndFilePendingBo#deleteByUserIdAndTyle(java.lang.Integer, java.lang.String)
	 */
	public void deleteByUserIdAndUrl(Integer userid,String url) {
		String sql = "delete from dbo.t_addresslistAndFile_pending where tjsuser_id="+userid+" and url='"+url+"' ";
		jdbcDao.executeUpdate(sql);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
	}
	
	public void deleteById(String id) {
		String sql = "delete from dbo.t_addresslistAndFile_pending where id='"+id+"' ";
		jdbcDao.executeUpdate(sql);
		jdbcDao.closeStmt();
		jdbcDao.closeConn();
	}
	
	public List findBySendUserIdAndTjsUserAndTitle(Integer senduserid,Integer tjsuserid, String title) {
		DetachedCriteria criteria = DetachedCriteria.forClass(AddressListAndFilePending.class);
		criteria.add(Restrictions.and(Restrictions.eq("tjsUserId", tjsuserid),Restrictions.and(Restrictions.eq("sendUserId", senduserid), Restrictions.eq("title", title))));
		criteria.addOrder(Order.desc("createTime"));
		
		List list = qbcDao.getCriteria(criteria);
		return list;
	}
}
